--region *.lua
--Date
--此文件由[BabeLua]插件自动生成

cc.exports.createBird = function()
    math.randomseed(os.time()) 
    local birdNo = math.random(3) - 1
    local bird = cc.Sprite:create("bird"..birdNo.."_0.png")
    local animation = cc.Animation:create()
    local name = ""

    for i = 0, 2 do
        name = "bird"..birdNo.."_"..i..".png"
        animation:addSpriteFrameWithFile(name)
    end
    animation:setLoops(65535)
    animation:setDelayPerUnit(0.1 / 3.0)
    animation:setRestoreOriginalFrame(true)
    local action = cc.Animate:create(animation)
    bird:runAction(action)

    local function addSwing()
        local up = cc.MoveBy:create(0.4, cc.p(0 ,8))
        local swing = cc.RepeatForever:create(cc.Sequence:create(up, up:reverse()))
        swing:setTag(TAG.SWING)
        bird:runAction(swing)
        bird:setTag(TAG.BIRD)
    end
    local function setPhysic()
        bird:setPhysicsBody(cc.PhysicsBody:createCircle(bird:getContentSize().width / 4, cc.PHYSICSBODY_MATERIAL_DEFAULT))
        bird:getPhysicsBody():setContactTestBitmask(1)
        bird:setPosition(cc.p(display.cx - bird:getContentSize().width * 1.5, display.cy + bird:getContentSize().height / 2))
        bird:getPhysicsBody():setGravityEnable(false)
    end
    local function getBird()
        return bird
    end

    return {addSwing = addSwing,
            setPhysic = setPhysic,
            getBird = getBird
    }
end

--endregion
